//
// Created by zhangzeyong on 2024/6/18.
//

#ifndef GUOXIN_JINZHENG_GLOBAL_FUNCTION_H
#define GUOXIN_JINZHENG_GLOBAL_FUNCTION_H

#include "global_define.h"

std::string GBKToUTF8(const std::string& gbkStr)
{
    return gbkStr;

    if (gbkStr.empty())
    {
        return "";
    }

    iconv_t cd = iconv_open("UTF-8", "GBK"); // 创建转换描述符
    if (cd == (iconv_t)-1)
    {
        // 如果iconv不可用，返回原字符串
        return gbkStr;
    }

    size_t inLen = gbkStr.size();
    size_t outLen = inLen * 4; // UTF-8 最多占用 4 字节
    char* inBuf = const_cast<char*>(gbkStr.c_str());
    char* outBuf = new char[outLen];
    char* outPtr = outBuf;

    size_t result = iconv(cd, &inBuf, &inLen, &outPtr, &outLen);
    iconv_close(cd);

    if (result == (size_t)-1)
    {
        delete[] outBuf;
        // 转换失败时返回原字符串
        return gbkStr;
    }

    std::string utf8Str(outBuf, outPtr - outBuf);
    delete[] outBuf;
    return utf8Str;
}

void ShowErrorInfo(int iRetCode) {
    printf("error code:%d", iRetCode);
    switch (iRetCode) {
        case -3:
            printf("invalid parameter\n");
            break;
        case -2:
            printf("invalid handle\n");
            break;
        case 100:
            printf("no data\n");
            break;
        case 101:
            printf("timeout\n");
            break;
        case 102:
            printf("exists\n");
            break;
        case 103:
            printf("more data\n");
            break;
        case 500:
            printf("call object function failed\n");
            break;
        case 501:
            printf("create object failed\n");
            break;
        case 502:
            printf("initialize object failed \n");
            break;
        case 503:
            printf("object uninitiated\n");
            break;
        case 504:
            printf("create resource failed\n");
            break;
        case 505:
            printf("dispatch event failed\n");
            break;
        case 506:
            printf("event  undefined \n");
            break;
        case 507:
            printf("register event {@1} from {@2} failed\n");
            break;
        case 508:
            printf("startup service {@1} failed\n");
            break;
        case 509:
            printf("init service env {@1} failed\n");
            break;
        case 510:
            printf("kernel/service env {@1} invalid\n");
            break;
        case 511:
            printf("service {@1} status not expect\n");
            break;
        case 512:
            printf("open internal queue {@1} failed\n");
            break;
        case 513:
            printf("open internal queue {@1} failed\n");
            break;
        case 514:
            printf("invalid message queue\n");
            break;
        case 515:
            printf("xml file {@1} format invalid\n");
            break;
        case 516:
            printf("open runtimedb {@1} failed\n");
            break;
        case 517:
            printf("create or initialize service function {@1}:{@2} fail \n");
            break;
        case 518:
            printf("option {@2} read only\n");
            break;
        case 519:
            printf("option {@2} unsupported \n");
            break;
        case 520:
            printf("purpose access {@2},but not granted\n");
            break;
        case 521:
            printf("queue {@1} fulled, max depth\n");
            break;
        case 522:
            printf("xa {@1} undefined\n");
            break;
        case 523:
            printf("call biz function {@1} exception\n");
            break;
        case 524:
            printf("timer {@1} callback failed, return\n");
            break;
        case 525:
            printf("filter expression {@1} invalid\n");
            break;
        case 526:
            printf("oem {@1} illegal\n");
            break;
        case 1000:
            printf("API��������\n");
            break;
        case 1001:
            printf("DLLȱʧ\n");
            break;
        case 1002:
            printf("DLL��ʼ��ʧ��(�汾����)\n");
            break;
        case 1003:
            printf("APIʵ���Ѵ���\n");
            break;
        case 1101:
            printf("insufficient space expect\n");
            break;
        case 1102:
            printf("receive packet from {@1} failed\n");
            break;
        case 1103:
            printf("send packet to {@1} failed\n");
            break;
        case 1104:
            printf("connect to {@1} failed\n");
            break;
        case 1105:
            printf("reconnect failed in function\n");
            break;
        case 1106:
            printf("reconnect {@1} success\n");
            break;
        case 1107:
            printf("disconnect\n");
            break;
        case 1100:
            printf("call zmq api {@2} failed\n");
            break;
        case 1200:
            printf("MA_ERROR_DB_EXCEPTION\n");
            break;
        case 1201:
            printf("data {@1} unload\n");
            break;
        case 1202:
            printf("table {@1} cursor {@2} has already opened\n");
            break;
        case 1203:
            printf("table {@1} cursor {@2} not opened\n");
            break;
        case 1204:
            printf("database {@1} not opened\n");
            break;
        case 1205:
            printf("invalid database connect string\n");
            break;
        case 1250:
            printf("MA_ERROR_DAO_EXCEPTION\n");
            break;
        case 1500:
            printf("call fix api {@2} failed\n");
            break;
        case 1501:
            printf("fix parse from {@1} failed\n");
            break;
        case 1502:
            printf("call kcbp api {@2} failed\n");
            break;
        case 1503:
            printf("invalid packet {@2} failed\n");
            break;
        case 1504:
            printf("call json api {@2} failed\n");
            break;
        case 1600:
            printf("call kcxp api {@2} failed\n");
            break;
        case 2000:
            printf("API�׽��ִ���\n");
            break;
        case 2001:
            printf("�ͻ�������ʧ��(�������Ӳ�����������Ƿ���)\n");
            break;
        case 2002:
            printf("����������ʧ��\n");
            break;
        case 3000:
            printf("API���ô���\n");
            break;
        case 3001:
            printf("GTU�ڵ������ļ�����\n");
            break;
        default:
            printf("������ϸ��Ϣ\n");
            break;
    }
}

#endif //GUOXIN_JINZHENG_GLOBAL_FUNCTION_H
